#!/bin/bash

bail() {
  echo -e "\033[31mError executing command, exiting.\033[0m"
  exit 1
}

exec_cmd_nobail() {
  echo "+ $1"
  bash -c "$1"
}

exec_cmd() {
  exec_cmd_nobail "$1" || bail
  wait
}

# 判断是否使用root运行
if [ "$UID" != "0" ]; then
  echo -e "\033[31mPlease run this script by root!\033[0m"
  bail
fi;


print_status() {
  local outp=$(echo "$1") # | sed -r 's/\\n/\\n## /mg')
  echo -e "## ${outp}"
}

if test -t 1; then # if terminal
  ncolors=$(which tput > /dev/null && tput colors) # supports color
  if test -n "$ncolors" && test $ncolors -ge 8; then
    termcols=$(tput cols)
    bold="$(tput bold)"
    underline="$(tput smul)"
    standout="$(tput smso)"
    normal="$(tput sgr0)"
    black="$(tput setaf 0)"
    red="$(tput setaf 1)"
    green="$(tput setaf 2)"
    yellow="$(tput setaf 3)"
    blue="$(tput setaf 4)"
    magenta="$(tput setaf 5)"
    cyan="$(tput setaf 6)"
    white="$(tput setaf 7)"
  fi
fi

print_bold() {
  title="$1"
  text="$2"

  echo
  echo "${red}================================================================================${normal}"
  echo "${red}================================================================================${normal}"
  echo -e "  ${bold}${yellow}${title}${normal}"
  echo -en "  ${text}"
  echo
  echo "${red}================================================================================${normal}"
  echo "${red}================================================================================${normal}"
}

# 绝对路径
ABSPATH=$(dirname $(readlink -f "$0"));

# source /etc/profile;
source "${ABSPATH}/env.sh"
source "${ABSPATH}/install.sh"

init() {
  exec_cmd "yum update -y"

  check_directory_is_exists
}

usage() {
  echo "Usage: $0 {web|updateWeb|ice}"
  exit 0
}

case "$1" in
  web)
    source "${ABSPATH}/web.sh"

    init
    init_web
    install_java
    install_node
    install_resin
    install_openresty
  ;;
  updateWeb)
    update_java
  ;;
  ice)
    source "${ABSPATH}/ice.sh"

    init
    init_ice
    install_java
  ;;
  test)
    echo "test"
  ;;
  *)
    usage
  ;;
esac;

